﻿--[[
    @Brief    帐号登录
    @Author    jameszhai
    @Date    2017-05-16
--]]

EAccLoginUI = 
{
    sAccPanel  = "pnlNewAcc";

    sAccView   = "pnlAccList";
    sEdtUsrAcc = "pnlAccList\\pnlUsrAcc\\edtUsrAcc";
    sIconUser  = "pnlAccList\\pnlUsrAcc\\icnUsrAcc";
    sLblAccTip = "pnlAccList\\pnlUsrAcc\\lblAccTips";

    sUserPwd   = "pnlUsrPwd\\edtUsrPwd";
    sIconPwd   = "pnlUsrPwd\\icnUsrPwd";

    sCbSavePwd = "cbSavePwd";
    sCbAtLogin = "cbAtLogin";
    sBtnLogin  = "btnLogin";
    sBnQKLogin = "btnQKLogin";
    sPnlLight  = "pnlLight";

    sPnlMoreTP = "pnlMoreLogin";
    sBtnFBAcc  = "pnlMoreLogin\\btnFacebook";
    sBtnWeChat = "pnlMoreLogin\\btnWeChat";
    sBtnMail   = "pnlMoreLogin\\btnMail";

    sDefIcon   = "ONEIMAGE:client_login\\qqacc_icon.png;Stretch;0";

};

--[[
// 点击了帐号
MSG_BEGIN(Msg_ClkAccItem)
    JSONB(int, uilib);
    JSONB(int, ctrl_id);
MSG_END

// 列表显示和隐藏
MSG_BEGIN(Msg_AccListShow)
    JSONB(int, show);            // 0 -- Hide  1 -- Show
MSG_END

]]

TAccLogin =
{
    InitView = function(dwRoot)
        TAccLogin.dwRootID = dwRoot;

        comm_center.listen_broadcast("Msg_ClkAccItem", "TAccLogin.OnClkAccItem");
        comm_center.listen_broadcast("Msg_AccListShow", "TAccLogin.OnAccListShow");
        comm_center.listen_broadcast("Msg_WebPitShow", "TAccLogin.OnAccListShow");

        ierd_tgp.tpf_ui.Require("wegame_login\\qrbutton.lua");
        ierd_tgp.tpf_ui.Require("wegame_login\\acc_list.lua");
    end;

    UnInitView = function()
        TQRButton.UnInitView();
        TAccComboBox.UnInitView();

        TAccLogin.bPwdChange = false;        -- 密码是否改变

        -- 界面控件
        TAccLogin.dwRootID   = 0;
        TAccLogin.pPnlAccUI  = nil;        -- 帐号页面
        TAccLogin.pEdtUsrPwd = nil;        -- 密码输入框

        TAccLogin.pCbSavePwd = nil;        -- 保存密码
        TAccLogin.pCbAtLogin = nil;        -- 自动登录

        TAccLogin.pBtnLogin  = nil;        -- 登录
        TAccLogin.pBtnBack   = nil;     -- 返回按钮
        TAccLogin.pBtnQKLogin = nil;    -- 快速登录
--        TAccLogin.pCapsTips  = nil;

        TAccLogin.ShowTEdit(false);
    end;

    -- 显示登录界面
    ShowView = function()
        if TAccLogin.CheckUI() and (nil ~= TAccLogin.pPnlAccUI) then
            TAccLogin.EnableFade(true);

            -- 显示界面
            TAccLogin.pPnlAccUI:Show(TRUE);

            -- 显示安全密码框
            TAccLogin.RegTASEvent();
            TAccLogin.ShowTEdit(true);

            -- 默认焦点设置到编辑框
            TAccLogin.pEdtUsrAcc:SetFocus();
            comm_center.call_service("Svr_ShowLoginNavTab", { show = true });
        end;
    end;

    -- 隐藏登录界面
    HideView = function()
        TAccLogin.EnableFade(false);
        TAccLogin.StopTimer();

        TAccLogin.ShowTEdit(false);
        TAccLogin.UnRegTASEvent();

        TQRButton.HideQRButton();

        if nil ~= TAccLogin.pPnlAccUI then            
            TAccLogin.pPnlAccUI:Show(FALSE);            
        end;
    end;

    EnableFade = function(bEnable)
        TAccLogin.bDoAnimate = bEnable;

        if not bEnable then
            TAccLogin.StopFade();
        end;
    end;

    ShowQRButton = function(bShow, bAni)
        TQRButton.ShowQRButton(bShow, bAni);
    end;

    -- 检查界面所有控件
    CheckUI = function()
        local dwRoot = ui_utility.get_ctrl_id_by_path(TAccLogin.dwRootID, 
                                                      EAccLoginUI.sAccPanel);

        TAccLogin.pPnlAccUI = ui_utility.get_ctrl_ptr_by_id(dwRoot);

        if nil == TAccLogin.pPnlAccUI then
            TGPLog.LogE("AccLogin", "Account page is null.");
            return false;
        end;

        if not TQRButton.InitView(dwRoot) then
            TGPLog.LogE("AccLogin", "QRButton init fail.");
            return false;
        end;

        TAccLogin.pNoQQTips = ui_utility.get_ctrl_ptr_by_path(dwRoot, "ttNoQQTips");

        if nil == TAccLogin.pNoQQTips then
            TGPLog.LogE("AccLogin", "Get no QQ detected tips control fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(TAccLogin.pNoQQTips:GetControlID(), "OnKillFocus",
                                     "TAccLogin.OnHideTips");

        -- 帐号输入框
        TAccLogin.pEdtUsrAcc  = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EAccLoginUI.sEdtUsrAcc);
        TAccLogin.nEdtUsrAcc = ui_utility.get_ctrl_id_by_path(dwRoot, 
                                    EAccLoginUI.sEdtUsrAcc);       
        if nil == TAccLogin.pEdtUsrAcc then
            TGPLog.LogE("AccLogin", "Edit user account is null.");
            return false;
        end;

        local     nEditID = TAccLogin.pEdtUsrAcc:GetControlID();

        ui_utility.setproperty_by_id(nEditID, "OnEditTextChange", "TAccLogin.OnEdtAccChange");
        ui_utility.setproperty_by_id(nEditID, "OnKeyDown", "TAccLogin.OnAccKeyDown");
        ui_utility.setproperty_by_id(nEditID, "OnSetFocus", "TAccLogin.OnAccSetFocus");
        ui_utility.setproperty_by_id(nEditID, "OnKillFocus", "TAccLogin.OnAccKillFocus");

        -- 发光
        TAccLogin.pPnlLight = ui_utility.get_ctrl_ptr_by_path(dwRoot, EAccLoginUI.sPnlLight);

        if nil == TAccLogin.pPnlLight then
            TGPLog.LogE("AccLogin", "Get login light button fail.");
            return false;
        end;
        TAccLogin.pPnlLight:Show(FALSE);

        TAccLogin.pLblAccTip = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EAccLoginUI.sLblAccTip);
        TAccLogin.nLblAccTip = ui_utility.get_ctrl_id_by_path(dwRoot,
                                    EAccLoginUI.sLblAccTip);

        if nil == TAccLogin.pLblAccTip then
            TGPLog.LogE("AccLogin", "Get account tips fail.");
            return false;
        end;

        -- 用户图标
        TAccLogin.nPnlUsrIcn = ui_utility.get_ctrl_id_by_path(dwRoot,
                                    EAccLoginUI.sIconUser);
        if nil == TAccLogin.nPnlUsrIcn or 0 == TAccLogin.nPnlUsrIcn then
            TGPLog.LogE("AccLogin", "User icon control is null.");
            return false;
        end;

        -- 帐号列表
        local     nTempID = ui_utility.get_ctrl_id_by_path(dwRoot, EAccLoginUI.sAccView);
        if not TAccComboBox.InitView(nTempID, TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin) then
            TGPLog.LogE("AccLogin", "Account list is null.");
            return false;
        end;

        -- 密码输入框
        TAccLogin.pEdtUsrPwd = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EAccLoginUI.sUserPwd);
        if nil == TAccLogin.pEdtUsrPwd then
            TGPLog.LogE("AccLogin", "Password control is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TAccLogin.pEdtUsrPwd:GetControlID(),
                                    "OnEditTextChange", "TAccLogin.OnPwdEdtChange");

        local     nPwdIcon = ui_utility.get_ctrl_id_by_path(dwRoot, 
                                    EAccLoginUI.sIconPwd);

        if nil == nPwdIcon or 0 == nPwdIcon then
            TGPLog.LogE("AccLogin", "Password icon control is null.");
            return false;
        end;

        ui_utility.setproperty_by_id(nPwdIcon, "OnMouseHover", "TAccLogin.OnEnterPwdIcon");
        ui_utility.setproperty_by_id(nPwdIcon, "OnMouseLeave", "TAccLogin.OnLeavePwdIcon");

        -- 保存密码
        TAccLogin.pCbSavePwd = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EAccLoginUI.sCbSavePwd);
        if nil == TAccLogin.pCbSavePwd then
            TGPLog.LogE("AccLogin", "Save password control is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TAccLogin.pCbSavePwd:GetControlID(), 
                                     "OnUnSignaled", "TAccLogin.OnSavePwdUnCheck");
        if TLoginModule.IsNetBar() then
            TAccLogin.pCbSavePwd:Show(FALSE);
        else
            TAccLogin.pCbSavePwd:Show(TRUE);
        end
        -- 自动登录
        TAccLogin.pCbAtLogin = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EAccLoginUI.sCbAtLogin);
        if nil == TAccLogin.pCbAtLogin then
            TGPLog.LogE("AccLogin", "Auto-login control is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TAccLogin.pCbAtLogin:GetControlID(), 
                                     "OnSignaled", "TAccLogin.OnAtLoginCheck");
        
        if TLoginModule.IsNetBar() then
            TAccLogin.pCbAtLogin:Show(FALSE);
        else
            TAccLogin.pCbAtLogin:Show(TRUE);
        end

        -- 登录按钮
        TAccLogin.pBtnLogin = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EAccLoginUI.sBtnLogin);
        if nil == TAccLogin.pBtnLogin then
            TGPLog.LogE("AccLogin", "Button login control is null.");
            return false;
        end;

        local nTempID = TAccLogin.pBtnLogin:GetControlID();
        
        ui_utility.setproperty_by_id(nTempID, "OnClick", "TAccLogin.OnClkLogin");
        ui_utility.setproperty_by_id(nTempID, "OnMouseHover", "TAccLogin.OnLoginEnter");
        ui_utility.setproperty_by_id(nTempID, "OnMouseLeave", "TAccLogin.OnLoginLeave");

        -- 快速登录
        local     dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EAccLoginUI.sBnQKLogin);
        TAccLogin.pBtnQKLogin = ui_utility.get_ctrl_ptr_by_id(dwTemp);
        if nil == dwTemp or 0 == dwTemp then
            TGPLog.LogE("AccLogin", "Button quick login fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(dwTemp, "OnClick", "TAccLogin.OnClkQKLogin");

        -- 大写锁定提示
        --[[
        TAccLogin.pCapsTips = ui_utility.get_ctrl_ptr_by_path(dwRoot, "btnCapsTip");

        if nil == TAccLogin.pCapsTips then
            TGPLog.LogE("AccLogin", "Get caps lock tips control fail.");
            return false;
        end;
        ]]


        if true == TAccLogin.bFirstShow then
            TAccLogin.SetTEditPwdMask(false);
            TAccLogin.bFirstShow = false;
        end

        TAccLogin.bPwdChange = false;

        if TAccLogin.CreateTEdit() then
            TAccLogin.ShowTEdit(true);
            return true;
        else
            TGPLog.LogE("AccLogin", "Create TEdit fail.");
            return false;
        end;
    end;

    -- 设置历史帐号列表
    SetAccList = function(zList)
        TAccLogin.zAccList = zList;
        TAccLogin.UpdateList();
    end;

    -- UIN是否保存密码
    IsSavedPwd = function(nUin)
        if nil == TAccLogin.zAccList then
            return false;
        end;

        for nIdx = 0, TAccLogin.zAccList.count - 1 do
            local     zItem = TAccLogin.zAccList.list[nIdx];

            if zItem.uin == nUin then
                return zItem.saved_pwd;
            end;
        end;

        return false;
    end;

    -- 点击用户登录
    OnClkLogin = function(pUILib, dwBtnID)
        TAccLogin.EnableFade(false);

        local     nOption = 0;
        local     bNetBar = TLoginModule.IsNetBar();

        if false == bNetBar then
            if nil ~= TAccLogin.pCbSavePwd and TRUE == TAccLogin.pCbSavePwd:IsChecked() then
                nOption = nOption + 1;
            end;

            if nil ~= TAccLogin.pCbAtLogin and TRUE == TAccLogin.pCbAtLogin:IsChecked() then
                nOption = nOption + 2;
            end;
        end;

        local     zParam = {};
        local     sInput = TAccLogin.GetCurUin();

        if (nil == sInput) or ("" == sInput) or (string.len(sInput) < 4) then
            if nil ~= TAccLogin.pEdtUsrAcc then
                TAccLogin.pEdtUsrAcc:SetFocus();
            end;

            -- 显示一个错误提示
            local     zParam = {};

            zParam["err_msg" ] = LoadStr("TPF_LOGIN_ACCERR_TIPS");
            zParam["err_code"] = 7610006;
            comm_center.call_service("Svr_ShowLoginErr", zParam);

            TGPLog.LogI("AccLogin", "Account less than 4.");

            return;
        end;

        zParam["uin"] = sInput;        -- 帐号
        zParam["opt"] = nOption;    -- Option

        -- 保存了密码
        if TLoginModule.eCurLoginType ~= TLoginModule.LOGIN_ACCOUNT_TYPE.KMailLogin and (false == bNetBar) and TAccLogin.IsSavedPwd(tonumber(sInput)) then
            if TAccLogin.bPwdChange then
                if TLoginModule.HasTEdit() then        -- 安全的登录模式
                    zParam["type"] = ELoginType.ltTEdit;        -- TEdit里面的密码登录
                else
                    zParam["type"] = ELoginType.ltAccLogin;
                    zParam["pwd"]  = TAccLogin.GetCurPwd();        -- 原始的帐号密码登录
                end;
            else
                zParam["type"] = ELoginType.ltSavedAcc;            -- 保存的密码登录
            end;
        else
            if TLoginModule.HasTEdit() then
                zParam["type"] = ELoginType.ltTEdit;            -- TEdit里面的密码登录
            else
                zParam["type"] = ELoginType.ltAccLogin;
                zParam["pwd"]  = TAccLogin.GetCurPwd();            -- 原始的帐号密码登录
            end;
        end;

        TAccLogin.sLastInput = sInput;
        comm_center.call_service("Svr_WGLogin", zParam);
    end;

    OnLoginEnter = function(pUILib, dwBtnID)
        TAccLogin.StartFade(false);
    end;

    OnLoginLeave = function(pUILib, dwBtnID)
        TAccLogin.StartFade(true);
    end;

    OnTimerFade = function()
        if not TAccLogin.bDoAnimate then
            TAccLogin.StopFade();
            return;
        end;

        local     nFadeStep = 50;

        if nil ~= TAccLogin.pPnlLight then
            local     bFinished = true;

            if TAccLogin.bIsFadeOut then
                TAccLogin.nFadeValue = TAccLogin.nFadeValue - nFadeStep;

                if TAccLogin.nFadeValue < 0 then
                    TAccLogin.nFadeValue = 0;
                else
                    bFinished = false;
                end;
            else
                TAccLogin.nFadeValue = TAccLogin.nFadeValue + nFadeStep;

                if TAccLogin.nFadeValue > 255 then
                    TAccLogin.nFadeValue = 255;
                else
                    bFinished = false;
                end;                
            end;

            ui_utility.setproperty_by_id(TAccLogin.pPnlLight:GetControlID(), "Alpha",
                                         tostring(TAccLogin.nFadeValue));

            if bFinished then
                TAccLogin.StopFade();
            end;
        else
            TGPLog.LogE("AccLogin", "FadeOut panel is null.");
        end;
    end;

    -- 开始淡入淡出
    StartFade = function(bFadeOut)
        if not TAccLogin.bDoAnimate then
            return;
        end;

        if TAccLogin.bIsFading and (bFadeOut == TAccLogin.bIsFadeOut) then
            return;
        end;

        if nil ~= TAccLogin.pPnlLight then
            TAccLogin.pPnlLight:Show(TRUE);
        end;

        if bFadeOut then
            TAccLogin.nFadeValue = 255;
        else
            TAccLogin.nFadeValue = 0;
        end;

        TAccLogin.bIsFadeOut = bFadeOut;

        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddInterval("TAccLogin.OnTimerFade", 20, 0);
        else
            TGPLog.LogE("AccLogin", "Start fade fail.");
        end;

        TAccLogin.bIsFading = true;
    end;

    -- 停止淡入淡出
    StopFade = function()
        if TAccLogin.bIsFading then
            local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveInterval("TAccLogin.OnTimerFade", 0);
            else
                TGPLog.LogE("AccLogin", "Stop fade fail");
            end;

            TAccLogin.bIsFading = false;
        end;

        if nil ~= TAccLogin.pPnlLight then
            if TAccLogin.bIsFadeOut then
                TAccLogin.pPnlLight:Show(FALSE);
            else
                ui_utility.setproperty_by_id(TAccLogin.pPnlLight:GetControlID(), "Alpha", "255");
            end;
        end;
    end;

    -- 点击快速登录
    OnClkQKLogin = function(pUILib, dwBtnID)
        local     zList = TLoginModule.GetQuickAccList();

        if nil == zList or zList.count <= 0 then
            TAccLogin.ShowTips(dwBtnID);
        else
            local     zParam = {};

            zParam["nPageIdx"] = ELoginPage.lpQKLogin;

            comm_center.call_service("Svr_SwitchLoginPage", zParam);
        end;
    end;

    -- TIPS
    ShowTips = function(dwBtnID)
        if nil ~= TAccLogin.pNoQQTips then
            TAccLogin.pNoQQTips:Show(TRUE);
            TAccLogin.StartTimer();
        end;
    end;

    OnHideTips = function()
        if nil ~= TAccLogin.pNoQQTips then
            TAccLogin.pNoQQTips:Show(FALSE);
        end;
    end;

    StartTimer = function()
        if TAccLogin.bShowTips then
            return;
        end;

        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddInterval("TAccLogin.OnHideTips", 2000, 0);
            TAccLogin.bShowTips = true;
        else
            TGPLog.LogE("AccLogin", "Start fade fail.");
        end;
    end;

    StopTimer = function()
        if TAccLogin.bShowTips then
            local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveInterval("TAccLogin.OnHideTips", 0);
                TAccLogin.bShowTips = false;
            else
                TGPLog.LogE("AccLogin", "Start fade fail.");
            end;
        end;
    end;

    -- 更新用户列表
    UpdateList = function()
        if not TAccComboBox.UpdateList(TAccLogin.zAccList) then
            TGPLog.LogI("AccLogin", "Update account list fail.");
            return;
        end;

        if (nil == TAccLogin.sLastInput or "" == TAccLogin.sLastInput) and 
           (TAccLogin.zAccList.count > 0) then
            TAccLogin.sLastInput = tostring(TAccLogin.zAccList.list[0].uin);
        end;

        if nil ~= TAccLogin.sLastInput and "" ~= TAccLogin.sLastInput then
            local     sText = U8toCUIStr(TAccLogin.sLastInput);
            TAccLogin.pEdtUsrAcc:SetText(sText);
            TAccLogin.OnUinChanged();
        end;
    end;

    -- 密码输入框
    OnPwdEdtChange = function()
        TAccLogin.bPwdChange = true;
        TGPLog.LogD("AccLogin", "Input edit text change.");
    end;

    -- 密码框
    OnAccKeyDown = function(pUILib, dwEdtID, dwVirtKey, dwKeyData)
        if 9 == dwVirtKey then        -- VK_TAB
            TAccLogin.OnTEditFocus();
        end;
    end;

    -- 输入的帐号变化
    OnEdtAccChange = function()
        if TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin == TLoginModule.eCurLoginType then
            TAccLogin.OnUinChanged();
        end
    end;

    -- 编辑框
    OnAccSetFocus = function()
        TAccLogin.bAccFocus = true;

        if nil ~= TAccLogin.pLblAccTip then
            TAccLogin.pLblAccTip:Show(FALSE);
        end;
    end;

    OnAccKillFocus = function()
        TAccLogin.bAccFocus = false;
        TAccLogin.CheckAccTips();
    end;

    CheckAccTips = function()
        if nil ~= TAccLogin.pLblAccTip then
            if TAccLogin.bAccFocus then
                TAccLogin.pLblAccTip:Show(FALSE);
                return;
            end;

            local     sTemp = TAccLogin.GetCurUin();

            if nil == sTemp or string.len(sTemp) <= 0 then
                TAccLogin.pLblAccTip:Show(TRUE);
            else
                TAccLogin.pLblAccTip:Show(FALSE);
            end;
        end;
    end;

    -- TEdit获得焦点
    OnTEditFocus = function()
           if TLoginModule.HasTEdit() then
            local     pLoginMgr = base_utility.get_component_adapter("ILogin_mgr",
                                "ierd_tgp.login_mgr.GetLoginMgr();");

            if nil == pLoginMgr then
                TGPLog.LogE("AccLogin", "Get login manager fail.");
                return;
            end;

            pLoginMgr:SetTeditFocus();
        else
            TGPLog.LogE("AccLogin", "TEdit get focus, edit is not created.")
        end;
    end;

    -- 帐号是否改变
    OnPwdChange = function(nEvtID, sJson)
        TAccLogin.bPwdChange = true;    -- 密码改变
    end;

    -- 帐号输入框设置焦点
    AccSetFocus = function()
        if nil ~= TAccLogin.pEdtUsrAcc then
            TAccLogin.pEdtUsrAcc:SetFocus();
        else
            TGPLog.LogE("AccLogin", "AccSetFocus, edit is null.");
        end;
    end;

    -- 检测状态
    CheckCapsStatus = function()
        -- 因为IsCapsLockOn函数不准确，使用内部变量来保存状态
        if nil == TAccLogin.bCapsLock then
            TAccLogin.bCapsLock = TAccLogin.IsCapsLockOn();
        else
            TAccLogin.bCapsLock = not TAccLogin.bCapsLock;
        end;

        TAccLogin.ShowCapsTips(TAccLogin.bCapsLock);
    end;

    -- 显示大写锁定状态提示
    OnPwdSetFocus = function()
        TAccLogin.bCapsLock = nil;
        TAccLogin.ShowCapsTips(true);
    end;

    -- 隐藏大写锁定状态
    OnPwdKillFocus = function()
        TAccLogin.ShowCapsTips(false);
    end;

    -- 显示安全提示
    ShowTASTips = function(bShow)
        if bShow then
            TAccLogin.ShowToolTips(LoadStr("STR_LOGIN_ACCPROTECT_ENABLED"));
        else
            comm_center.call_service("Svr_HideToolTips", {});
        end;
    end;

    ShowToolTips = function(sText)
        local     zParam = {};

        zParam["left"] = 614;
        zParam["top"]  = 198;
        zParam["text"] = sText;

        comm_center.call_service("Svr_ShowToolTips", zParam);
    end;

    OnEnterPwdIcon = function()
        TAccLogin.ShowTASTips(true);
    end;

    OnLeavePwdIcon = function()
        TAccLogin.ShowTASTips(false);
    end;

    IsCapsLockOn = function()
        local     pLoginMgr = base_utility.get_component_adapter("ILogin_mgr",
                            "ierd_tgp.login_mgr.GetLoginMgr();");

        if nil == pLoginMgr then
            TGPLog.LogE("AccLogin", "CapsTips, Get login manager fail.");
            return;
        end;

        return pLoginMgr:IsCapsLockOn();
    end;

    -- 显示大写锁定提示
    ShowCapsTips = function(bShow)
        if bShow and TAccLogin.IsCapsLockOn() then
            TAccLogin.ShowToolTips(LoadStr("STR_LOGIN_CAPSLOCK_ON"));
        else
            comm_center.call_service("Svr_HideToolTips", {});
        end;
    end;

    -- TAS消息
    OnTASMessage = function(nEvtID, sJson)
        local zParam = base_utility.json_to_table(sJson);

        if nil == zParam then
            TGPLog.LogE("AccLogin", "TASMessage, param is null.");
            return;
        end;

        local nMsgID = zParam.msp;
        local wParam = zParam.wparam;

        if 0x07 == nMsgID then              -- WM_SETFOCUS
            TAccLogin.OnPwdSetFocus();
        elseif 0x08 == nMsgID then          -- WM_KILLFOCUS
            TAccLogin.OnPwdKillFocus();
        elseif 0x104 == nMsgID then
            if 0x73 == wParam then
                comm_center.call_service("Svr_ExitWeGame", {});
            end;
        elseif 0x102 == nMsgID then  -- WM_CHAR
            if 0x9 == wParam then          -- VK_TAB
                TAccLogin.AccSetFocus();
            elseif 0x0d == wParam then  -- VK_RETURN
                TAccLogin.OnClkLogin(nil, 0);
            elseif 0x14 == wParam then     -- VK_CAPITAL
                TAccLogin.CheckCapsStatus();
            end
        end;
    end;

    -- 创建TEdit
    CreateTEdit = function()
        if TLoginModule.HasTEdit() then
            TAccLogin.RegTASEvent();
            TGPLog.LogI("LoginM", "TEdit already created.");
            return true;
        end;

        if (nil ~= TAccLogin.pEdtUsrPwd) then
            local     rtTemp = TAccLogin.pEdtUsrPwd:GetRectEx(TRUE,TRUE);

            if TLoginModule.CreateTEdit(rtTemp) then
                return true;
            else
                return false;
            end;
        else
            TGPLog.LogE("LoginV", "Create TEdit fail.");
            return false;
        end;
    end;

    -- 销毁TEdit
    DestroyTEdit = function()
        if TLoginModule.HasTEdit() then
            -- 反注册事件
            TAccLogin.UnRegTASEvent();

            -- 销毁EDIT
            TLoginModule.DestroyTEdit();
        else
            TGPLog.LogI("AccLogin", "TEdit already destroy.");
        end;
    end;

    RegTASEvent = function()
        if TAccLogin.bRegEvent then
            return;
        end;

        local     pTPFUI =  base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_MESSAGE, 
                                  "TAccLogin.OnTASMessage");
            pTPFUI:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_PSW_CHANGED, 
                                  "TAccLogin.OnPwdChange");
            TAccLogin.bRegEvent = true;
        else
            TGPLog.LogE("AccLogin", "Get TPF UI fail.");
        end;
    end;

    UnRegTASEvent = function()
        if TAccLogin.bRegEvent then
            local     pTPFUI =  base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_MESSAGE, 
                                           "TAccLogin.OnTASMessage");
                pTPFUI:RemoveLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_PSW_CHANGED, 
                                           "TAccLogin.OnPwdChange");
                TAccLogin.bRegEvent = false;
            else
                TGPLog.LogE("AccLogin", "Destroy TEdit, Get TPF UI fail.");
            end;
        end;
    end;

    -- 显示TEdit
    ShowTEdit = function(bShow)
        if TLoginModule.HasTEdit() then
            TLoginModule.ShowTEdit(bShow);

            if nil ~= TAccLogin.pEdtUsrPwd then
                if bShow then
                    TAccLogin.pEdtUsrPwd:Show(FALSE);
                else
                    TAccLogin.pEdtUsrPwd:Show(TRUE);
                end;
            end;
        else
            TGPLog.LogI("AccLogin", "TEdit not created.");
        end;
    end;

    -- 设置状态
    SetStatus = function(zParam)
    end;

    OnAccListShow = function(zParam)
        if 1 == zParam["show"] then
            TAccLogin.ShowTEdit(false);
        else
            TAccLogin.ShowTEdit(true);
        end;
    end;

    -- 点击消息处理
    OnClkAccItem = function(zParam)
        if nil ~= zParam and zParam.login_type == TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin then
            TAccLogin.OnClkItem(ui_utility.uilib(), zParam["ctrl_id"]);
        else
            TGPLog.LogE("AccLogin", "Click item, param is null.");
        end;
    end;

    -- 帐号变化
    OnClkItem = function(pUILib, nBtnID)
        if nil ~= pUILib and nil ~= nBtnID then
            local     pBtn = pUILib:GetControlPtr(nBtnID);

            if nil == pBtn then
                TGPLog.LogE("AccLogin", "Get account button fail.");
                return;
            end;

            if nil == TAccLogin.pEdtUsrAcc then
                TGPLog.LogE("AccLogin", "User account edit is null.");
                return;
            end;

            local     pTmp = tolua.cast(pBtn, "Tenio::ITButton");
            local     sUin = pTmp:GetCaption();

            if nil ~= sUin then
                TAccLogin.pEdtUsrAcc:SetText(sUin);
                TAccLogin.OnUinChanged();
            else
                TGPLog.LogE("AccLogin", "Get button text fail.");
            end;            
        else
            TGPLog.LogE("AccLogin", "Button click, UI lib is null.");
        end;
    end;

    -- 从密码输入框获取密码
    GetCurPwd = function()
        if nil ~= TAccLogin.pEdtUsrPwd then
            local     sText = TAccLogin.pEdtUsrPwd:GetText();

            if nil == sText then
                TGPLog.LogE("AccLogin", "Input password is null.");
                return nil;
            end;

            local     sTemp = CUIStrtoU8(sText);
            return     sTemp;
        else
            TGPLog.LogE("AccLogin", "Password edit is null.");
            return nil;
        end;
    end;

    -- 获取当前输入的UIN
    GetCurUin = function()
        if nil == TAccLogin.pEdtUsrAcc then
            TGPLog.LogE("AccLogin", "User account edit is null.");
            return nil;
        end;

        local     sUin = TAccLogin.pEdtUsrAcc:GetText();

        if nil ~= sUin then
            local     sTemp = CUIStrtoU8(sUin);
            return sTemp;
        else
            return nil;
        end;
    end;

    -- UIN变化
    OnUinChanged = function()
        local     sUin = TAccLogin.GetCurUin();

        if (nil == sUin) or ("" == sUin) then
            if nil ~= TAccLogin.pCbSavePwd then
                TAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
            end;

            if nil ~= TAccLogin.pCbAtLogin then
                TAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
            end;
            TAccLogin.SetTEditPwdMask(false);

            ui_utility.setproperty_by_id(TAccLogin.nPnlUsrIcn, "Background", 
                                         EAccLoginUI.sDefIcon);

            TGPLog.LogI("AccLogin", "Get current UIN fail.");

            if nil ~= TAccLogin.pLblAccTip then
                if TAccLogin.bAccFocus then
                    TAccLogin.pLblAccTip:Show(FALSE);
                else
                    TAccLogin.pLblAccTip:Show(TRUE);
                end;
            end;

            return;
        end;

        if nil == TAccLogin.zAccList then
            TGPLog.LogE("AccLogin", "Account list is null.");
            return;
        end;

        if nil == TAccLogin.pCbSavePwd then
            TGPLog.LogE("AccLogin", "Save password control is null.");
            return;
        end;

        if nil ~= TAccLogin.pLblAccTip then
            TAccLogin.pLblAccTip:Show(FALSE);
        end;

        local     nUin = tonumber(sUin);
        local     bFind = false;

        for nIdx = 0, TAccLogin.zAccList.count - 1 do
            local     zTemp = TAccLogin.zAccList.list[nIdx];

            if nil ~= zTemp and zTemp.uin == nUin then
                if TLoginModule.IsNetBar() then
                    TAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
                    TAccLogin.SetTEditPwdMask(false);

                    if nil ~= TAccLogin.pLblAccTip then
                        TAccLogin.pLblAccTip:Show(FALSE);
                    end;
                else
                    if zTemp.saved_pwd then
                        TAccLogin.pCbSavePwd:SetCheckedNoSignal(TRUE);
                        TAccLogin.SetTEditPwdMask(true);
                    else
                        TAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
                        TAccLogin.SetTEditPwdMask(false);
                    end;
                end;

                -- 去掉自动登录标志
                if nil ~= TAccLogin.pCbAtLogin then
                    TAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
                end;

                if nil ~= zTemp.head and "" ~= zTemp.head then
                       ui_utility.setproperty_by_id(TAccLogin.nPnlUsrIcn, "Background", 
                                           "ONEIMAGE:" .. zTemp.head .. ";Circle;0;");
                else
                       ui_utility.setproperty_by_id(TAccLogin.nPnlUsrIcn, "Background", 
                                            EAccLoginUI.sDefIcon);
                   end;

                TAccLogin.bPwdChange = false;
                bFind = true;
                break;
            end;
        end;

        -- 没有保存的帐号，设置密码为空，自动登录、保存密码为未选中
        if not bFind then
            TAccLogin.SetTEditPwdMask(false);

            if nil ~= TAccLogin.pCbSavePwd then
                TAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
            end;

            if nil ~= TAccLogin.pCbAtLogin then
                TAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
            end;

               ui_utility.setproperty_by_id(TAccLogin.nPnlUsrIcn, "Background", 
                                            EAccLoginUI.sDefIcon);
        end;
    end;

    -- 设置是否有保存密码标志
    SetTEditPwdMask = function(bMask)
        local     pLoginMgr = base_utility.get_component_adapter("ILogin_mgr",
                            "ierd_tgp.login_mgr.GetLoginMgr();");

        if nil == pLoginMgr then
            TGPLog.LogE("AccLogin", "Get login manager fail.");
            return;
        end;

        if TLoginModule.HasTEdit() then
            pLoginMgr:SetTeditPasswordMask(bMask);
            TGPLog.LogI("AccLogin", "Set TEdit password mask: " .. tostring(bMask));
        elseif nil ~= TAccLogin.pEdtUsrPwd then
            local     sTemp = nil;

            if bMask then
                sTemp = U8toCUIStr("1234567890");
            else
                sTemp = U8toCUIStr("");
            end;

            TAccLogin.pEdtUsrPwd:SetText(sTemp);
        end;
    end;

    OnSavePwdUnCheck = function()
        if nil ~= TAccLogin.pCbAtLogin then
            TAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
        end;
    end;

    OnAtLoginCheck = function()
        if nil ~= TAccLogin.pCbSavePwd then
            TAccLogin.pCbSavePwd:SetCheckedNoSignal(TRUE);
        end;
    end;


    -- Private:
    zAccList   = nil;        -- 帐号列表
    bPwdChange = false;        -- 密码是否改变
    sLastInput = nil;        -- 最后一次输入的帐号
    pLblAccTip = nil;        -- 输入账号提示
    bAccFocus  = false;        -- 编辑框是否有焦点
    bRegEvent  = false;
    strCurMailAcc = "";

    -- 界面控件
    dwRootID   = 0;
    pPnlAccUI  = nil;        -- 帐号页面
    pEdtUsrPwd = nil;        -- 密码输入框
    nEdtUsrAcc = 0;
    nPnlUsrIcn = nil;        -- 用户图标

    pPnlLight  = nil;        -- 发光

    pNoQQTips  = nil;        -- 没有检测到QQ提示
    bShowTips  = false;

    nFadeValue = 0;
    bIsFadeOut = false;
    bDoAnimate = false;

    pCbSavePwd = nil;        -- 保存密码
    pCbAtLogin = nil;        -- 自动登录

--    pCapsTips  = nil;        -- 大写锁定控件
    bCapsLock  = nil;        -- 大写状态

    pBtnQKLogin = nil;      -- 快速登录
    pBtnLogin  = nil;        -- 登录
    pBtnBack   = nil;       -- 返回按钮
    nLblAccTip = 0;         -- tips
    bFirstShow = true;
};